<template>
  <el-container>
    <el-header>
      <div class="logo">
        <img src="../assets/heima.png" alt>
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="goback">退出</el-button>
    </el-header>
    <!-- 页面主体部分 -->
    <el-container class="mainbox">
      <!-- 左侧边导航栏 -->
      <el-aside :width="isCollapse?'60px':'200px'">
        <div class="collapsebtn" @click="isCollapse=!isCollapse">|||</div>
        <!-- 侧边栏菜单 -->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          :default-active="activePath"
          router
        >
          <!-- 一级菜单 -->
          <el-submenu :index="'/'+item.path" v-for="item in menuList" :key="item.id">
            <!-- 一级菜单模板 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconList[item.id]"></i>
              <!-- 文本 -->
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/'+subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveNavState('/'+subItem.path)"
            >
              <!-- 二级菜单模板 -->
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体结构 -->
      <el-main>
        <!-- 主体内容占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data() {
    return {
      menuList: [],
      isCollapse: false,
      iconList: {
        "125": "iconfont icon-user",
        "103": "iconfont icon-tijikongjian",
        "101": "iconfont icon-shangpin",
        "102": "iconfont icon-danju",
        "145": "iconfont icon-baobiao"
      },
      activePath: ""
    };
  },
  created() {
    this.getMenuList();
    this.activePath = window.sessionStorage.getItem("activePath");
  },
  methods: {
    // 退出按钮
    goback() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    // 获取导航栏列表数据
    async getMenuList() {
      const { data: res } = await this.$http.get("menus");
      // console.log(res);
      if (res.meta.status !== 200) {
        return this.$message.error("获取菜单列表失败");
      }
      this.$message.success("获取菜单列表成功");
      this.menuList = res.data;
      // console.log(res.data);
    },
    saveNavState(activePath) {
      window.sessionStorage.setItem("activePath", activePath);
      this.activePath = activePath;
    }
  }
};
</script>
<style lang="less" scoped>
.el-header {
  display: flex;
  background-color: #373d41;
  color: #fff;
  font-size: 20px;
  line-height: 60px;
  justify-content: space-between;
  align-items: center;
  .logo {
    display: flex;
    align-items: center;
    > span {
      margin-left: 20px;
    }
  }
}
.el-aside {
  background-color: #333744;
}

.el-main {
  background-color: #eaedf1;
}
.el-menu {
  border: 0;
}
.collapsebtn {
  width: 100%;
  height: 25px;
  background-color: rgb(173, 173, 173);
  text-align: center;
  line-height: 25px;
}
.iconfont {
  margin-right: 15px;
}
</style>

